﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class LoadKey : System.Web.UI.Page
{
    static int count = 0;
    static byte[] KEY = new byte[32];
    static byte[] IV = new byte[16];
    static bool hasloadkey = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void loadkey_Click(object sender, EventArgs e)
    {

        if (hasloadkey)
        {
            HZ.Web.MessageBox.Show("密钥已经加载！");
        }
        // 处理
        switch (count)
        {
            case 0:
                btnloadkey.Text = "加载密钥1";
                break;
            case 1:
                btnloadkey.Text = "加载密钥2";
                break;
            case 2:
                btnloadkey.Text = "加载密钥3";
                break;
            case 3:
                btnloadkey.Text = "验证KEY";
                break;
            default:
                btnloadkey.Text = "验证KEY";
                break;
        }

        if (count < 3)
        {
            try
            {
                string path = System.Configuration.ConfigurationManager.AppSettings["keypath"];
                if (!System.IO.File.Exists(path))
                {
                    HZ.Web.MessageBox.Show("密钥路径不存在，请重试！");
                    return;
                }
                Stream s = File.OpenRead(path);
                if (s.Length < 48) // 文件大小不够，直接退出。
                {
                    s.Close();
                    HZ.Web.MessageBox.Show("密钥长度不对，请确定是否为该密钥");
                }
                BinaryReader br = new BinaryReader(s);
                byte[] buf_A = new byte[32]; byte[] buf_B = new byte[16];
                buf_A = br.ReadBytes(32); buf_B = br.ReadBytes(16);
                for (int i = 0; i < 32; i++)
                {
                    KEY[i] ^= buf_A[i];
                }
                for (int i = 0; i < 16; i++)
                {
                    IV[i] ^= buf_B[i];
                }
            }
            catch
            {
                HZ.Web.MessageBox.Show("配置项keypath不存在，请指定密钥路径");
            }
        }
        else
        {
            hasloadkey = true;
            HZ.Encrypt.EncryptWrapper.Key = KEY;
            HZ.Encrypt.EncryptWrapper.IV = IV;
            HZ.Encrypt.EncryptWrapper.SetKey = true;
            HZ.Web.WebUtil.Alert("密钥加载成功", this);
            // 加载KEY
        }
        count++;
        HZ.Web.WebUtil.Alert("操作成功", this);
        // Response.Write("加载的数据KEY");
        //Response.Write(count);
        //Response.Write("<br />");

        //Response.Write(HZ.MathUtil.ByteToHexStr(KEY));
        //Response.Write("<br />");
        //Response.Write(HZ.MathUtil.ByteToHexStr(IV));
    }
    protected void btnResetKey_Click(object sender, EventArgs e)
    {
        //count = 0;
        //KEY = new byte[32];
        //IV = new byte[16];
        //hasloadkey = false;
        //btnloadkey.Text = "加载密钥1";
    }
}
